var http = require("http");
var fs = require('fs');
var url = require('url');

 
    http.createServer(function(request, response) {
        var pathname = url.parse(request.url).pathname;
        var ext = pathname.match(/(\.[^.]+|)$/)[0]; // 获取文件名后缀
        
        switch(ext){
            case ".css":
            case ".js":
                fs.readFile("../src"+request.url, 'utf-8',function (err, data) {
                    if (err) throw err;
                    response.writeHead(200, {
                        "Content-Type": {
                             ".css":"text/css",
                             ".js":"application/javascript",
                      }[ext]
                    });
                    response.write(data);
                    response.end();
                });
                break;
             case  '.html':
              fs.readFile("../src"+request.url, 'utf-8',function (err, data) {
                    if (err) throw err;
                    response.writeHead(200, {
                        "Content-Type": "text/html"
                    });
                    response.write(data);
                    response.end();
                });
              break;
              case '.png':
              fs.readFile("../src"+request.url, 'utf-8',function (err, data) {
                    if (err) throw err;
                    response.writeHead(200, {
                        "Content-Type": "image/png"
                    });
                    response.write(data);
                    response.end();
                });
              break;
              case '.jpg':
              fs.readFile("../src"+request.url, 'utf-8',function (err, data) {
                    if (err) throw err;
                    response.writeHead(200, {
                        "Content-Type": "image/jpg"
                    });
                    response.write(data);
                    response.end();
                });
              break;
              case '.gif':
fs.readFile("../src"+request.url, 'utf-8',function (err, data) {
                    if (err) throw err;
                    response.writeHead(200, {
                        "Content-Type": "image/gif"
                    });
                    response.write(data);
                    response.end();
                });
              break;
            default:
               break;
        }
 
    }).listen(8888);
    console.log("server start at port 8888");
 